﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web.Script.Serialization;
using LiuYan.Base;

namespace LiuYan.Interface
{
    public class WxAccess_Token
    {
        /// <summary>
        /// 获取access_token
        /// </summary>
        /// <returns></returns>
        public string GetAccessToken()
        {
            string temp = "";
            WxEntity.getAccessTokenUrl = string.Format(WxEntity.getAccessTokenUrl, WxEntity.appid, WxEntity.secret);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(WxEntity.getAccessTokenUrl);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using (Stream resStream = response.GetResponseStream())
            {
                StreamReader reader = new StreamReader(resStream, Encoding.Default);
                temp = reader.ReadToEnd();
                resStream.Close();
            }
            JavaScriptSerializer Jss = new JavaScriptSerializer();
            Dictionary<string, object> respDic = (Dictionary<string, object>)Jss.DeserializeObject(temp);
            string access_token = respDic["access_token"].ToString();
            return access_token;
            return "";
        }
    }
}
